import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Data } from '@angular/router';
import { map, switchMap, tap } from 'rxjs/operators';
import { Observable } from 'rxjs/internal/Observable';
import { Model, ModelPermission, Pageable, PermissionEnum, SysroleModelPermissionPK } from 'src/app/domain/sparrow';
import { ModelService } from 'src/app/service/model.service';
import { SysroleSearchComponent } from 'src/app/share/sysrole-search/sysrole-search.component';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-model-permission',
  templateUrl: './model-permission.component.html',
  styleUrls: ['./model-permission.component.css']
})
export class ModelPermissionComponent implements OnInit {

  listOfCurrentPageData: readonly Data[] = [];
  pageable: Pageable = {page: 0, size: 10}
  total: number = 0;
  permissionType!: string;

  constructor(private modelService: ModelService) {

  }

  ngOnInit(): void {

  }

  // re-get the data
  onPermissionTypeChange(event: string){
    console.log(event)
    switch (event) {
      case "USER":
        //
        break;


      case "SYSROLE":
        this.modelService.getSysroleModelPermission(this.pageable).subscribe(res=>{
          this.listOfCurrentPageData = res.content;
          this.total = res.totalElements;
        })
        break;



      default:
        break;
    }
  }

  onCurrentPageDataChange(listOfCurrentPageData: readonly Data[]): void {
    this.listOfCurrentPageData = listOfCurrentPageData;
  }
  onPageIndexChange(page: number) {
    this.pageable.page = page;
    this.modelService.list(this.pageable).subscribe(res => {
      this.total = res.totalElements;
      this.listOfCurrentPageData = res.content;
      // this.listOfData = res.content;
    })
  }

  onPageSizeChange(size: number) {
    this.pageable.size = size;
    this.modelService.list(this.pageable).subscribe(res => {
      this.total = res.totalElements;
      this.listOfCurrentPageData = res.content;
      // this.listOfData = res.content;
    })
  }
}
